/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazEditarArea.java
 *
 * Created on 27/10/2011, 10:29:54 PM
 */
package presentacion;

import Paneles.PanelGeneral;
import business.AreaBO;
import dominio.Area;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

/**
 *
 * @author vanessa
 */
public class InterfazEditarArea extends javax.swing.JFrame {

    private DefaultComboBoxModel dcb = new DefaultComboBoxModel();
    private AreaBO bo = new AreaBO();
    private Area ar = new Area();
    private int ok = 0;
    private ImageIcon icono;

    /** Creates new form InterfazEditarArea */
    public InterfazEditarArea() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
        comboAreas.setModel(dcb);
        cargarListaAreas();
        entrada1.setEditable(false);
        entrada2.setEditable(false);
        b_modificar.setEnabled(false);
        eliminarArea.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        comboAreas = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        entrada1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        entrada2 = new javax.swing.JTextField();
        b_modificar = new javax.swing.JButton();
        eliminarArea = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();

        setTitle("Edición de Areas");
        setResizable(false);

        jLabel1.setText("Busque un Area Existente:");

        comboAreas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                comboAreasMouseClicked(evt);
            }
        });
        comboAreas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboAreasActionPerformed(evt);
            }
        });

        jLabel2.setText("Nuevo Nombre:");

        jLabel3.setText("Descripción:");

        b_modificar.setText("Modificar");
        b_modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_modificarActionPerformed(evt);
            }
        });

        eliminarArea.setText("Eliminar");
        eliminarArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarAreaActionPerformed(evt);
            }
        });

        cancelar.setText("Cancelar");
        cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(entrada2, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
                            .addComponent(comboAreas, 0, 417, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(entrada1, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE))
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(b_modificar, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(eliminarArea, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addComponent(cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(comboAreas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(entrada1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(entrada2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b_modificar)
                    .addComponent(eliminarArea))
                .addGap(18, 18, 18)
                .addComponent(cancelar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_modificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_modificarActionPerformed
        Area tst;
        ok=0;
        if (entrada1.getText().equals("") || entrada2.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe digitar los datos completos para modificar un Area", null, JOptionPane.ERROR_MESSAGE);
        }
        else {
            tst = bo.consultarArea(entrada1.getText() + "");

            if (tst == null||(comboAreas.getSelectedItem()+"").equals(entrada1.getText())) {
                ok = bo.actualizarArea(comboAreas.getSelectedItem() + "", entrada1.getText(), entrada2.getText());
                entrada1.setText("");
                entrada2.setText("");//mar
            }
            if (ok == 1) {
                JOptionPane.showMessageDialog(this, "La modificación del area fue exitosa", "Modificación exitosa !!!", JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                if (tst != null) {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al modificar el area (Nombres repetidos)", "Modificación fallida", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al modificar el area", "Modificación fallida", JOptionPane.ERROR_MESSAGE);
                }
            }
            // this.setVisible(false);t
            cargarListaAreas();
        }
        //this.setVisible(false);
    }//GEN-LAST:event_b_modificarActionPerformed

    private void eliminarAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarAreaActionPerformed
        //        this.setVisible(false);
        if (comboAreas.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(this, "No hay areas que borrar", "Eliminación fallida", JOptionPane.ERROR_MESSAGE);
        }
        else {
            ok = bo.encontrarArea(comboAreas.getSelectedItem() + "");
            if (ok != 0) {
                bo.borrarArea(ok);
                JOptionPane.showMessageDialog(this, "Eliminado correctamente", "Eliminación correcta", JOptionPane.OK_OPTION);
                cargarListaAreas();
            }
        }
    }//GEN-LAST:event_eliminarAreaActionPerformed

    private void comboAreasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboAreasActionPerformed
        b_modificar.setEnabled(true);
        eliminarArea.setEnabled(true);
        entrada1.setEditable(true);
        entrada2.setEditable(true);
        Area area = bo.consultarArea((String)comboAreas.getSelectedItem());
        entrada1.setText(area.getNombreArea());
        entrada2.setText(area.getDescripcionArea());
    }//GEN-LAST:event_comboAreasActionPerformed

    private void comboAreasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_comboAreasMouseClicked
        /*     int indiceLista = comboAreas.getSelectedIndex();
        if (indiceLista != -1) {
        //ar = bo.instanciarArea(comboAreas.getSelectedObjects());
        ar = (Area) comboAreas.getSelectedItem();
        entrada1.setText(ar.getNombreArea());
        entrada2.setText(ar.getDescripcionArea());
        //entrada1.setText("nada");
        // entrada2.setText("xP");
        }*/
    }//GEN-LAST:event_comboAreasMouseClicked

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancelarActionPerformed

    /**
     * Este metodo permite que los nombres de las areas que se encuentran
     * en la BD aparezcan en el comboBox (comboAreas)
     */
    private void cargarListaAreas() {
        List l = bo.consultarAreas();
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) comboAreas.getModel();
        modelo.removeAllElements();
        for (int i = 0; i < l.size(); i++) {
            modelo.addElement(l.get(i));

        }
        comboAreas.setModel(modelo);

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazEditarArea().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_modificar;
    private javax.swing.JButton cancelar;
    private javax.swing.JComboBox comboAreas;
    private javax.swing.JButton eliminarArea;
    private javax.swing.JTextField entrada1;
    private javax.swing.JTextField entrada2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
}
